package com.zhuiyun.project.api.memberTypeManagement.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.memberTypeManagement.entity.MemberTypeManagement;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeIdModel;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeManagementModel;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeModel;
import com.zhuiyun.project.api.memberTypeManagement.service.MemberTypeService;
import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName MemberTypeController
 * @Description TODO 会员类型管理控制层
 * @Author WX
 * @Date 2023/11/2 11:36
 **/
@RestController
@Api(tags = "会员类型管理")
@Slf4j
@CrossOrigin
@RequestMapping("/member")
public class MemberTypeController{
    @Autowired
    private MemberTypeService memberTypeService;
    /**
    * @Author WX
    * @Description 获取会员类型信息
    * @Date 2023/11/2 13:52
    * @Return
    * @param pageRequest memberTypeManagementModel
    */
    @ApiOperation(value = "获取会员类型信息")
    @OptLog(modular = "会员类型管理", type = CommonConstants.SELECT, describe = "select")
    @GetMapping("/getMemberType")
    public CommonResult getMemberType(PageRequest pageRequest, MemberTypeManagementModel memberTypeManagementModel) {
        return CommonResult.ok(memberTypeService.getMemberType(pageRequest,memberTypeManagementModel));
    }
    /**
    * @Author WX
    * @Description 通过id获取会员类型信息
    * @Date 2023/11/2 15:20
    * @Return
    * @param memberTypeIdModel
    */
    @ApiOperation(value = "通过id获取会员类型信息")
    @OptLog(modular = "会员类型管理", type = CommonConstants.SELECT, describe = "select")
    @PutMapping("/getById")
    public CommonResult getById(@RequestBody MemberTypeIdModel memberTypeIdModel) {
        Integer id =memberTypeIdModel.getId();
         String venueUniqueIdentifier = memberTypeIdModel.getVenueUniqueIdentifier();
        MemberTypeManagement memberTypeManagement = memberTypeService.getById(id,venueUniqueIdentifier);
        return CommonResult.ok(memberTypeManagement);
    }
    /**
    * @Author WX
    * @Description 添加会员类型信息
    * @Date 2023/11/2 15:58
    * @Return
    * @param memberTypeModel
    */
    @ApiOperation(value = "添加会员类型信息")
    @OptLog(modular = "会员类型管理", type = CommonConstants.ADD, describe = "add")
    @PostMapping("/addMember")
    public CommonResult addMember (@RequestBody MemberTypeModel memberTypeModel) throws CommonException {
        return CommonResult.ok(memberTypeService.addMember(memberTypeModel));
    }
    /**
    * @Author WX
    * @Description 通过id删除会员类型信息
    * @Date 2023/11/2 19:02
    * @Return
    * @param memberTypeIdModel
    */
    @ApiOperation(value = "通过id删除会员类型信息")
    @OptLog(modular = "会员类型管理", type = CommonConstants.DELETE, describe = "delete")
    @PutMapping("/delMember")
    public CommonResult delMember(@RequestBody MemberTypeIdModel memberTypeIdModel) {
        Integer id =memberTypeIdModel.getId();
        String venueUniqueIdentifier = memberTypeIdModel.getVenueUniqueIdentifier();
        return CommonResult.ok(memberTypeService.delMember(id,venueUniqueIdentifier));
    }
    /**
    * @Author WX
    * @Description 通过id修改会员类型信息
    * @Date 2023/11/2 19:19
    * @Return
    * @param memberTypeModel
    */
    @ApiOperation(value = "通过id修改会员类型信息")
    @OptLog(modular = "会员类型管理", type = CommonConstants.UPDATE, describe = "update")
    @PostMapping("/updateMemberById")
    public CommonResult updateMemberById (@RequestBody MemberTypeModel memberTypeModel) throws CommonException {
        return CommonResult.ok(memberTypeService.updateMemberById(memberTypeModel));
    }
    /**
    * @Author WX
    * @Description 修改会员状态
    * @Date 2023/11/2 19:32
    * @Return
    * @param memberTypeIdModel
    */
    @ApiOperation(value = "修改会员状态")
    @OptLog(modular = "会员类型管理" , type = CommonConstants.UPDATE, describe = "修改会员状态")
    @PutMapping("/updateEnableStatus")
    public CommonResult updateEnableStatus (@RequestBody MemberTypeIdModel memberTypeIdModel) {
        // 提取出字段
        Integer memberTypeId = memberTypeIdModel.getId();
        String venueUniqueIdentifier =memberTypeIdModel.getVenueUniqueIdentifier();
        Integer enableStatus =memberTypeIdModel.getEnableStatus();
        // 查询是否有会员用户数据
        boolean b = memberTypeService.getByMemberTypeId(memberTypeId,venueUniqueIdentifier);
        if (enableStatus == 1 && b){
            return CommonResult.ok("null");
        }else {
            return CommonResult.ok(memberTypeService.updateEnableStatus(memberTypeIdModel));
        }
    }
}
